import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
// import * as types from './types.js'
import 'whatwg-fetch'
export const baseUrl = 'http://test.api.meirenguvip.com';
export const baseImgUrl = 'http://img.meirenguvip.com/';

// fetch.defaults.baseURL = testApiUrl;
// "/mb/itemClass/show"
const myFetch = async(url,fn,{method="GET",headers={},body="",query={}} = {}) => {
	let config,_query='';url=baseUrl+url;
	switch(method.toLowerCase()){
		case "get":
			config = {method:method};
			for(let i in query){
				_query += i + '=' + query[i] + '&'
			}
			url+="?"+_query
	};
	try{
		let res = await fetch(url,config).then((res) => {
			if (res.status >= 200 && res.status < 300) {
				return res.json()
			}
			return Promise.reject(new Error(res.status));
		})
		fn(res)
		console.log('wait 222222222222');
	}catch(e){
		console.log('!!!!*** error here ***!!!! ',e)
	}
}


const store = new Vuex.Store({
	state:{
		name:'zhaodi',
		aysnDate:{
			orderNavList:null,
			itemProShow:null
		}
	},
	mutations:{
		['ORDER_NAV_LIST'](state,upDate){
			state.aysnDate.orderNavList = upDate;
		},
		['ITEM_PRO_SHOW'](state,upDate){
			state.aysnDate.itemProShow = upDate;
		}
	},
	actions:{
		order_nav_list(context,upDate){
			myFetch("/mb/itemClass/show",function(res){
				console.log('wait 1111111111111111');
				context.commit('ORDER_NAV_LIST',res.data.list)
			})
		},
		item_pro_show(context,upDate){
			return myFetch("/mb/itemPro/show",function(res){
				context.commit('ITEM_PRO_SHOW',res.data.list)
			},{query:upDate})
		}
	}
})

export default store;
